terminalwork = new Vue({
	el: "#terminalwork",
	data: {
		isShowList: false,
		currentUser: '', // 用户信息

		terminalKeyword: '', // 关键字
		currentPage: 1, // 当前页
		terminalworkList: [], // 定期工作列表
		// 码表
		checkResultTypeList:[],
		troubleWorkTypeList:[],
		troubleStatusList:[],
		
		defectInfo:{},
		// 缺陷详情相关
		troubleRecordInfoVisible:false, // 展示缺陷详情
		defectInfoInfo:{},
		selectArr:[],
		defectInfoList: [],
		defectInfos: [],//储存初始查出来的列表数据
		// 关闭相关
		troubleRecordCloseVisible:false,
		defectInfoClose:{},
		defectInfoCloseInfo: {
			closeStaffCodeName: '',
			closeTime: '',
			upclose: ''
		},
		// 调整上报状态相关
		troubleRecordChangeVisible:false,
		troubleChangeList:[],
		
		// 新增
		troubleRecordAddVisible:false,
		defectAddInfo:{
			defectId: '',//缺陷隐患记录主键
			workId: '',//工作主键
			workType: '',//工作种类
			defectDesc: '',//缺陷隐患描述
			url: '',//安全云跳转地址
			reportStaffCode: '',//填报人编号
			reportStaffName: '',//填报人姓名
			reportTime: '',//填报时间
			closeStaffCode: '',//关闭人编号
			closeStaffName: '',//关闭人姓名
			closeTime: '',//关闭时间
			workSheetCode: '',//关闭工作票
			closeCause: '',//关闭原因
			upReport: '',//上报状态 0:否 1:是
			upStaffCode: '',//上报人编号
			upStaffName: '',//上报人姓名
			remark: '',//备注
			client: '',//客户ID
			dataSource: 'NEIOS_APP'+localStorage.getItem("currVersion"),//数据来源
			status: '',//业务状态 0:未提交 1:待整改 2:已闭环 3:处理中
			dataStatus: '',//是否有效 参见码表0删除&1正常
			createBy: '',//创建人员
			createStaffName: '',//创建人姓名
			createDate: '',//创建时间
			modifyBy: '',//修改人员
			modifyStaffName: '',//修改人员姓名
			modifyDate: '',//修改时间
			reportStaffCodeName: ''
		}
	},
	components: {
		personselect,
		multipleselect
	},
	mounted: function() {
		// 获取用户信息
		this.currentUser = JSON.parse(localStorage.getItem('neiosUser'))
		// 获取码表 
		let dictCodes = 'YES_OR_NO,TROUBLE_WORK_TYPE,TROUBLE_STATUS';
		let _this = this
		let dict = mui.neiosCommon.queryDict(dictCodes, function(data) {
			if (data != '') {
				_this.checkResultTypeList = data.YES_OR_NO
				_this.troubleWorkTypeList = data.TROUBLE_WORK_TYPE
				_this.troubleStatusList = data.TROUBLE_STATUS
			} 
			// 获取列表 
			_this.queryTerminalList(_this.currentPage);
		})
	},
	methods: {
		// 码表转换
		dictChange (list,data) {
			return mui.neiosCommon.dictTransform(list,data)
		},
		// 隐藏操作按钮
		hideOperBtn() {
			$('.todo').css('width', '0');
			$('.toAppoint').css('width', '0');
		},
		// 查询列表
		queryTerminalList(current) {
			this.hideOperBtn()
			let url = '/app/troubleRecord/neiosDefect/getTroubleRecord'
			let _this = this
			let param = {
				pageIndex: current,
				pageSize: 10,
				searchType :'0',
				keyWord:this.terminalKeyword,
				areaCompanyCode:this.currentUser.companyNewCode,
				stationCode:localStorage.getItem('neiosStationCode'),
				role:this.currentUser.roles,
			}
			console.log("param",JSON.stringify(param))
			mui.neiosRequst.httpRequst(url, param,
				function(data) {
					console.log("data参数",JSON.stringify(data))
					if (data.code == '0') {
						_this.isShowList = true
						if (_this.currentPage == 1) {
							mui('#terminalworkList').pullRefresh().enablePullupToRefresh();
							mui('#terminalworkList').pullRefresh().endPullupToRefresh();
							_this.terminalworkList = data.data.records
						} else {
							mui('#terminalworkList').pullRefresh().endPullupToRefresh();
							_this.terminalworkList = _this.terminalworkList.concat(data.data.records)
						}
						if (data.data.current >= data.data.pages) {
							console.log("====================禁用====================")  
							mui('#terminalworkList').pullRefresh().disablePullupToRefresh();
							mui('#terminalworkList').pullRefresh().endPullupToRefresh(true);
						}
					} else {
						_this.terminalworkList = []
						mui.toast('查询列表失败，请稍后重试！')
					}
				},
				function() {
					_this.terminalworkList = [] 
					mui.toast('查询列表失败，请稍后重试！')
				}
			);
		},
		// 关键字搜索
		terminalKeywordChange() {
			this.currentPage = 1
			this.queryTerminalList(1)
			this.isShowList = false
		},
		
		// 缺陷详情 start
		openTroubleRecordInfo (row) {
			let _this = this
			_this.hideOperBtn()
			_this.defectInfo = JSON.parse(JSON.stringify(row))
			_this.defectInfoInfo = JSON.parse(JSON.stringify(row))
			_this.troubleRecordInfoVisible = true
			
			if (_this.defectInfoInfo.workType == '0401') {
				_this.selectArr = [_this.defectInfoInfo.upReportText];
				_this.defectInfoInfo.reportStaffCodeName = `[${_this.defectInfoInfo.reportStaffCode}]${_this.defectInfoInfo.reportStaffName}`;
				if (_this.defectInfoInfo.closeStaffCode != null && _this.defectInfoInfo.closeStaffCode != '') {
					_this.defectInfoInfo.closeStaffCodeName = `[${_this.defectInfoInfo.closeStaffCode}]${_this.defectInfoInfo.closeStaffName}`;
				}
			}
			if (_this.defectInfoInfo.workType != '0401') {
				let params = {
					defectId: _this.defectInfoInfo.defectId
				}
				_this.queryDefectInfo(params);
			}
		},
		// 获取缺陷详情
		queryDefectInfo (params) {
			let _this = this;
			_this.tableLoading = true; 
			mui.neiosRequst.httpRequst('/app/troubleRecord/neiosDefect/getDefectInfo', params,
				function(data) { 
					if (data.code == '0') {
						_this.defectInfoList = data.data;
						_this.defectInfos = JSON.parse(JSON.stringify(_this.defectInfoList));
					} else {
						mui.toast('查询失败，请稍后重试！')
					}
				},
				function() {
					mui.toast('查询失败，请稍后重试！')
				}
			);
		},
		//关闭详情界面
		closeDefectInfo() {
			let _this = this;
			_this.troubleRecordInfoVisible = false;
			_this.defectInfoInfo = {};
			_this.selectArr = [];
			_this.defectInfoList = [];
			_this.defectInfos = [];//储存初始查出来的列表数据
			_this.defectInfo = {}
		},
		// 确认
		defectInfoComfirm () {
			let _this = this;
			let newDefectInfoList = JSON.parse(JSON.stringify(_this.defectInfoList));
			newDefectInfoList.map((v, i) => {
				if (v.defectInfoCause != _this.defectInfos[i].defectInfoCause) {
					v.modifyBy = _this.currentUser["userId"];
					v.modifyStaffName = _this.currentUser["name"];
					v.modifyDate = mui.neiosCommon.transTime(new Date());
				}
			})
			let params = {
				infoListParam: newDefectInfoList
			}
			mui.neiosRequst.httpRequst('/app/troubleRecord/neiosDefect/saveOrUpdateDefectInfo', params,
				function(data) { 
					if (data.code == '0') {
						_this.closeDefectInfo();
						_this.currentPage = 1
						_this.queryTerminalList(1)
						mui.toast('保存成功！')
					} else {
						mui.toast('保存失败，请稍后重试！')
					}
				},
				function() {
					mui.toast('保存失败，请稍后重试！')
				}
			);
		},
		// 缺陷详情 end
		
		// 关闭 start
		// 打开弹窗
		openTroubleRecordClose (row) {
			let _this = this
			_this.hideOperBtn()
			_this.defectInfo = JSON.parse(JSON.stringify(row))
			_this.defectInfoClose = JSON.parse(JSON.stringify(row))
			_this.troubleRecordCloseVisible = true;
			_this.defectInfoCloseInfo.closeStaffCodeName = `[${_this.currentUser["userId"]}]${_this.currentUser["name"]}`
			_this.defectInfoCloseInfo.closeTime = mui.neiosCommon.transTime(new Date());
		},
		// 关闭
		closeInfoClose () {
			let _this = this
			_this.troubleRecordCloseVisible = false;
			_this.defectInfoCloseInfo = {
				closeStaffCodeName: '',
				closeTime: '',
				upclose: ''
			}
			_this.defectInfo = {}
			_this.defectInfoClose = {}
		},
		// 校验
		checkcloseCause (e) {
			if (this.defectInfoCloseInfo.closeCause == '' || this.defectInfoCloseInfo.closeCause == null || this.defectInfoCloseInfo.closeCause == undefined) {
				$('textarea[name='+e+']').addClass('erro')
			} else {
				$('textarea[name='+e+']').removeClass('erro')
			}
		},
		// 确定
		closeComfirm () {
            let _this = this;
			if (this.defectInfoCloseInfo.closeCause == '' || this.defectInfoCloseInfo.closeCause == null || this.defectInfoCloseInfo.closeCause == undefined) {
				mui.toast('请输入关闭原因！')
				return
			} else {
				_this.defectInfoClose.closeStaffCode = _this.currentUser["userId"];
				_this.defectInfoClose.closeStaffName = _this.currentUser["name"];
				_this.defectInfoClose.closeTime = _this.defectInfoCloseInfo.closeTime;
				_this.defectInfoClose.closeCause = _this.defectInfoCloseInfo.closeCause;
				_this.defectInfoClose.status = '2';
				_this.defectInfoClose.modifyBy = _this.currentUser["userId"];
				_this.defectInfoClose.modifyStaffName = _this.currentUser["name"];
				_this.defectInfoClose.modifyDate = mui.neiosCommon.transTime(new Date());
				let infoList = [];
				infoList.push(_this.defectInfoClose);
				let params = {
					infoListParam: infoList
				}
				mui.neiosRequst.httpRequst('/app/troubleRecord/neiosDefect/saveOrUpdateDefect', params,
					function(data) { 
						if (data.code == '0') {
							_this.closeInfoClose();
							_this.currentPage = 1
							_this.queryTerminalList(1)
							mui.toast('关闭成功！')
						} else {
							mui.toast('关闭失败 ，请稍后重试！')
						}
					},
					function() {
						mui.toast('关闭失败，请稍后重试！')
					}
				);
			}
		},
		// 关闭 end
		
		// 调整上报状态  start
		// 开启调整弹窗
		openTroubleRecordChange (item) {
			this.hideOperBtn()
			this.troubleRecordChangeVisible = true;
			this.troubleChangeList.push(JSON.parse(JSON.stringify(item)))
		},
		//关闭
		changeClose () {
			this.troubleRecordChangeVisible = false
			this.troubleChangeList = []
		},
		// 确认
		changeComfirm () {
			let _this = this
			let change = $('input[name="reoubleChangeRadio"]')
			let changeVal = ''
			let isChange = false
			for (let item of change) {
				if (item.checked) {
					changeVal = item.value
					isChange = true
				}
			}
			if (isChange) {
				_this.troubleChangeList.map(v => {
					v.upReport = changeVal;
					v.reportStaffCode = _this.currentUser["userId"];
					v.reportStaffName = _this.currentUser["name"];
					v.reportTime = mui.neiosCommon.transTime(new Date());
					v.modifyBy = _this.currentUser["userId"];
					v.modifyStaffName = _this.currentUser["name"];
					v.modifyDate = mui.neiosCommon.transTime(new Date());
				})
				let params = {
					infoListParam: _this.troubleChangeList
				}
				mui.neiosRequst.httpRequst('/app/troubleRecord/neiosDefect/saveOrUpdateDefect', params,
					function(data) { 
						if (data.code == '0') {
							_this.changeClose();
							_this.currentPage = 1
							_this.queryTerminalList(1)
							mui.toast('修改成功！')
						} else {
							mui.toast('修改失败，请稍后重试！')
						}
					},
					function() {
						mui.toast('修改失败，请稍后重试！')
					}
				);
			} else {
				mui.toast('请选择是否已上报！')
				return false
			}
		},
		// 调整上报状态  end
		
		// 新增  start
		openTroubleRecordAdd () {
			this.hideOperBtn()
			let _this = this
			_this.troubleRecordAddVisible = true
			_this.defectAddInfo.reportStaffCodeName = `[${_this.currentUser["userId"]}]${_this.currentUser["name"]}`
			_this.defectAddInfo.reportTime = mui.neiosCommon.transTime(new Date());
		},
		// 关闭
		addClose () {
			let _this = this
			_this.troubleRecordAddVisible = false
			_this.defectAddInfo = { 
				defectId: '',//缺陷隐患记录主键
				workId: '',//工作主键
				workType: '',//工作种类 
				defectDesc: '',//缺陷隐患描述
				url: '',//安全云跳转地址
				reportStaffCode: '',//填报人编号
				reportStaffName: '',//填报人姓名
				reportTime: '',//填报时间
				closeStaffCode: '',//关闭人编号
				closeStaffName: '',//关闭人姓名
				closeTime: '',//关闭时间
				workSheetCode: '',//关闭工作票
				closeCause: '',//关闭原因
				upReport: '',//上报状态 0:否 1:是
				upStaffCode: '',//上报人编号
				upStaffName: '',//上报人姓名
				remark: '',//备注
				client: '',//客户ID
				dataSource: 'NEIOS_APP'+localStorage.getItem("currVersion"),//数据来源
				status: '',//业务状态 0:未提交 1:待整改 2:已闭环 3:处理中
				dataStatus: '',//是否有效 参见码表0删除&1正常
				createBy: '',//创建人员
				createStaffName: '',//创建人姓名
				createDate: '',//创建时间
				modifyBy: '',//修改人员
				modifyStaffName: '',//修改人员姓名
				modifyDate: '',//修改时间
				reportStaffCodeName: ''
			}
		},
		// 校验
		checkAddDesc (e) {
			if (this.defectAddInfo.defectDesc == '' || this.defectAddInfo.defectDesc == null || this.defectAddInfo.defectDesc == undefined) {
				$('input[name='+e+']').addClass('erro')
			} else {
				$('input[name='+e+']').removeClass('erro')
			}
		},
		// 确定
		addComfirm () {
			let _this = this
			if (this.defectAddInfo.defectDesc == '' || this.defectAddInfo.defectDesc == null || this.defectAddInfo.defectDesc == undefined) {
				mui.toast('请输入隐患描述！')
				return 
			}
			let change = $('input[name="addRadio"]')
			let changeVal = ''
			let isChange = false
			for (let item of change) {
				if (item.checked) {
					changeVal = item.value
					isChange = true
				}
			}
			if (!isChange) {
				mui.toast('请选择是否已上报！')
				return 
			}
			_this.defectAddInfo.upReport = changeVal
			_this.defectAddInfo.defectId = mui.neiosCommon.getUuidv4();
			_this.defectAddInfo.workType = '0401';
			_this.defectAddInfo.reportStaffCode = _this.currentUser["userId"];
			_this.defectAddInfo.reportStaffName = _this.currentUser["name"];
			if (_this.defectAddInfo.upReport == '1') {
				_this.defectAddInfo.upStaffCode = _this.currentUser["userId"];
				_this.defectAddInfo.upStaffName = _this.currentUser["name"];
			}
			_this.defectAddInfo.client = 'NEIOS';
			_this.defectAddInfo.dataSource = 'NEIOS_APP'+localStorage.getItem("currVersion");
			_this.defectAddInfo.status = '1';
			_this.defectAddInfo.dataStatus = '1';
			_this.defectAddInfo.createBy = _this.currentUser["userId"];
			_this.defectAddInfo.createStaffName = _this.currentUser["name"];
			_this.defectAddInfo.createDate = mui.neiosCommon.transTime(new Date());
			_this.defectAddInfo.modifyBy = _this.currentUser["userId"];
			_this.defectAddInfo.modifyStaffName = _this.currentUser["name"];
			_this.defectAddInfo.modifyDate = mui.neiosCommon.transTime(new Date()); 
			let infoList = [];
			infoList.push(_this.defectAddInfo);
			let params = {
				infoListParam: infoList
			}
			mui.neiosRequst.httpRequst('/app/troubleRecord/neiosDefect/saveOrUpdateDefect', params,
				function(data) { 
					if (data.code == '0') {
						_this.addClose();
						_this.currentPage = 1
						_this.queryTerminalList(1)
						mui.toast('新增成功！')
					} else {
						mui.toast('新增失败，请稍后重试！')
					}
				},
				function() {
					mui.toast('新增失败，请稍后重试！')
				}
			);
		},
		// 新增  end
		// 跳转详情
		goDetail (row) {
			this.hideOperBtn()
			let url = ''
			if (row.workType == '0101') { // 蓄电池
				url = 'details/storBatteryDet.html'
			} else if (row.workType == '0102' || row.workType == '0103'
			 || row.workType == '0104' || row.workType == '0105'
			 || row.workType == '0106' || row.workType == '0107'
			 || row.workType == '0108') { // 场用电、UPS定期检查、事故照明定期检查、主变风冷定期检查、直流切换定期检查、消防切换定期检查、通信定期检查
				url = 'details/powerSystemDet.html'
			} else if (row.workType == '0109') { // 柴油机
				url = 'details/dieselGerDet.html'
			} else if (row.workType == '0110' || row.workType == '0207') { // 工器具
				url = 'details/toolCheckDet.html'
			} else if (row.workType == '0111') { // 安全设施定期
				url = 'details/safeDeviceDet.html' 
			} else if (row.workType == '0113') { // 生活消防水设备定期检查
				url = 'details/lifeWaterDet.html' 
			} else if (row.workType == '0114') { // 设备红外定期检查
				url = 'details/infraredDet.html' 
			} else if (row.workType == '0115') { // 网络安全防护设备定期检查
				url = 'details/netSafeDeviceDet.html' 
			} else if (row.workType == '0117') { // 避雷器动作记录
				url = 'details/lightningArresterDet.html' 
			} 
			let param = {
				obj: row,
				alwinType: 0
			}
			localStorage.setItem("neiosGoDetail", JSON.stringify(param))
			mui.openWindow({
				url: url,
				id: url,
				extras: {},
				waiting: {
					autoShow: false
				}
			});
		},
	}
});
// 显示操作按钮
function showOperBtn(el) {
	$('.todo').css('width', '0');
	$(el).next().css('width', '73vw');
}
mui.init({
	pullRefresh: {
		container: "#terminalworkList", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		down: { //下拉刷新
			style: 'circle',
			height: 50, //可选,默认50.触发下拉刷新拖动距离,
			auto: false, //可选,默认false.首次加载自动下拉刷新一次
			contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
			contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
			contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
			callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				terminalwork.currentPage = 1;
				terminalwork.queryTerminalList(1);
				////没有更多内容了，endPulldown 传入true， 不再执行下拉刷新
				mui('#terminalworkList').pullRefresh().endPulldownToRefresh();
				mui('#terminalworkList').pullRefresh().enablePullupToRefresh();
				mui('#terminalworkList').pullRefresh().endPullupToRefresh(true);
			}
		},
		up: { //上拉加载
			height: 50, //可选.默认50.触发上拉加载拖动距离
			auto: false, //可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				terminalwork.currentPage = terminalwork.currentPage * 1 + 1;
				terminalwork.queryTerminalList(terminalwork.currentPage);
			}
		}
	},
});
mui.plusReady(function() {
	window.addEventListener('isRefresh', function() {
		window.location.reload()
	})
})
mui('.mui-input-clear')[0].addEventListener('focus', function() {
	mui(".mui-icon-clear")[0].addEventListener('tap', function() {
		terminalwork.terminalKeyword = ''
		terminalwork.queryTerminalList(1);
	})
})
